package com.data.unityplugin_market.base

import android.content.Context
import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.data.unityplugin_market.base.BaseDataBindingAdapter.BaseViewHolder
import com.data.unityplugin_market.viewmodel.MenuItemVM

public open class BaseDataBindingAdapter<T>() : RecyclerView.Adapter<BaseViewHolder>() {
    var context: Context? = null
    var inflater: LayoutInflater? = null
    var layoutId: Int = 0
    var variableId: Int = 0
    var list: ArrayList<T> ?= null


    /*
     * 短按监听
     */
    var onRecyclerViewItemClickListener: OnRecyclerViewItemClickListener ? = null;
    /**
     * 长按监听
     */
    var onRecyclerViewItemLongClickListener: OnRecyclerViewItemLongClickListener? = null;


    constructor(context: Context, inflater: LayoutInflater, layoutId: Int, variableId: Int, list: ArrayList<T>) : this() {
        this.context = context;
        this.inflater = inflater;
        this.layoutId = layoutId;
        this.variableId = variableId;
        this.list = list;
    }


    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): BaseViewHolder {
        var binding: ViewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(p0.context), layoutId, p0, false)
        var holder = BaseViewHolder(binding.root, onRecyclerViewItemClickListener, onRecyclerViewItemLongClickListener)
        holder.binding = binding
        return holder;
    }


    override fun getItemCount(): Int {
        return list!!.size;
    }

    override fun onBindViewHolder(p0: BaseViewHolder, p1: Int) {
        p0.binding?.setVariable(variableId,list!!.get(p1))
        p0.binding?.executePendingBindings()
    }


    open class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener, View.OnLongClickListener {
        override fun onClick(v: View?) {
            itemClick?.onItemClick(adapterPosition, v!!)
        }

        override fun onLongClick(v: View?): Boolean {
            return itemLongClick?.onItemLongClick(adapterPosition, v!!)!!
        }

        var binding: ViewDataBinding? = null;

        var itemClick: OnRecyclerViewItemClickListener? = null
        var itemLongClick: OnRecyclerViewItemLongClickListener? = null

        constructor(itemView: View, itemClick: OnRecyclerViewItemClickListener?, itemLongClick: OnRecyclerViewItemLongClickListener?) : this(itemView) {
            this.itemClick = itemClick;
            this.itemLongClick = itemLongClick;
            itemView.setOnClickListener(this)
            itemView.setOnLongClickListener(this)
        }


    }

    public interface OnRecyclerViewItemClickListener {
        fun onItemClick(position: Int, view: View)
    }

    public interface OnRecyclerViewItemLongClickListener {
        fun onItemLongClick(position: Int, view: View): Boolean
    }
}